#include "ScrollView.h"

#include "../_Luly.h"
ScrollView::ScrollView(std::string uiObjectId, ScrollView::Alignment alignment)
    : UIObject(uiObjectId), alignment(alignment) {
  layout = new Layout("scrollV_" + uiObjectId, Layout::Arrangement::VERTICAL);
  layout->setHeightMode(UIObject::WHMode::FIT);
  UIObject::addChild(layout);
}
void ScrollView::init() { UIObject::init(); }

void ScrollView::event() {
  SDL_Event& event = _Luly::getInstance().current_event;

#ifndef _ANDROID_PLATFORM__
  int mouseX = 0;
  int mouseY = 0;
  SDL_GetMouseState(&mouseX, &mouseY);
  mouseX = int(mouseX / _Luly::getInstance().scale);
  mouseY = int(mouseY / _Luly::getInstance().scale);
  if (event.wheel.type == SDL_MOUSEWHEEL) {
    if (isPointWithin(mouseX, mouseY)) {
      if (alignment == Alignment::Top) {
        current_deltaY = ScrollView::y - layout->getY();
        if (current_deltaY - 4 * event.wheel.preciseY <= maxDeltaY) {
          if (current_deltaY - 4 * event.wheel.preciseY < 0) {
            layout->setRelativeY(0);
          } else {
            layout->setY(layout->getY() + 4 * event.wheel.preciseY);
          }
        } else {
          layout->setY(y - maxDeltaY);
        }
      } else {
        current_deltaY = layout->getY() + layout->getHeight() - (y + height);
        if (current_deltaY + 4 * event.wheel.preciseY <= maxDeltaY) {
          if (current_deltaY + 4 * event.wheel.preciseY < 0) {
            layout->setY(y - maxDeltaY);

          } else {
            layout->setY(y - (maxDeltaY - current_deltaY) +
                         4 * event.wheel.preciseY);
          }
        } else {
          layout->setRelativeY(0);
        }
      }
    }
  }

#else
  // bool couldChildEvent = true;
  if (event.tfinger.type == SDL_FINGERMOTION) {
    //如果有垂直移动，那么就移动，并禁止子对象的事件
    if (event.tfinger.dy != 0) {
      //如果移动的手指在fingeIds里面
      if (fingerIds.find(event.tfinger.fingerId) != fingerIds.end()) {
        // couldChildEvent = false;  //禁止子对象事件
        //移动视图

        //如果是上对齐
        if (alignment == Alignment::Top) {
          current_deltaY = ScrollView::y - layout->getY();
          if (current_deltaY -
                  event.tfinger.dy * _Luly::getInstance().windowWidth <=
              maxDeltaY) {
            if (current_deltaY -
                    event.tfinger.dy * _Luly::getInstance().windowWidth <
                0) {
              layout->setRelativeY(0);
            } else {
              layout->setY(layout->getY() +
                           event.tfinger.dy * _Luly::getInstance().windowWidth);
            }
          } else {
            layout->setY(y - maxDeltaY);
          }
        } else {
          //如果是底部对齐
          current_deltaY = layout->getY() + layout->getHeight() - (y + height);
          if (current_deltaY +
                  event.tfinger.dy * _Luly::getInstance().windowWidth <=
              maxDeltaY) {
            if (current_deltaY +
                    event.tfinger.dy * _Luly::getInstance().windowWidth <
                0) {
              layout->setY(y - maxDeltaY);

            } else {
              layout->setY(y - (maxDeltaY - current_deltaY) +
                           event.tfinger.dy * _Luly::getInstance().windowWidth);
            }
          } else {
            layout->setRelativeY(0);
          }
        }
      }
    }
  }
  switch (event.tfinger.type) {
    case SDL_FINGERDOWN: {
      //按下时，如果手指在范围内，则记录该手指
      if (isPointWithin(event.tfinger.x * _Luly::getInstance().windowWidth,
                        event.tfinger.y * _Luly::getInstance().windowHeight)) {
        fingerIds.insert(event.tfinger.fingerId);
      }
      break;
    }
    case SDL_FINGERUP: {
      //抬起时，移除手指(set无需判断是否在里面，如果不在的话，只是返回end())
      fingerIds.erase(event.tfinger.fingerId);
      // if (fingerIds.size() == 0) {
      //   couldChildEvent = true;
      // }else{
      //   couldChildEvent = false;
      // }

      break;
    }
  }

    // if (couldChildEvent) {
    //   //如果为true
    //   UIObject::event();  //执行子对象事件操作
    // }
#endif
  UIObject::event();
}

void ScrollView::drawArea(const Rect& area) {
  //同步位置
  layout->setX(x);
  if (alignment == Alignment::Top) {
    layout->setY(y - current_deltaY);
  } else {
    layout->setY(y - (maxDeltaY - current_deltaY));
  }

  //如果不在允许的范围内，则强制移回范围内
  if (ScrollView::y - layout->getY() <= maxDeltaY) {
    if (ScrollView::y - layout->getY() < 0) {
      if (alignment == Alignment::Top) {
        layout->setRelativeY(0);
      } else {
        layout->setY(y - maxDeltaY);
      }
    }
  } else {
    if (alignment == Alignment::Bottom) {
      layout->setY(y - maxDeltaY);
    } else {
      layout->setRelativeY(0);
    }
  }

  //更新maxDeltaY
  int tempDeltaY = layout->getHeight() - height;
  maxDeltaY = (tempDeltaY < 0) ? 0 : tempDeltaY;

  width = layout->getWidth();
  if (layout->getHeight() > parent_object->getContentHeight()) {
    ScrollView::heightMode = WHMode::FILL;
  } else {
    if (ScrollView::heightMode != WHMode::FILL) {
      ScrollView::height = layout->getHeight();
    }
  }

  //子对象只绘制在scrollView里的部分
  UIObject::drawArea(area);  //只传入area就行了，UIObject会自动处理传入子对象的area
}